import {
  text,
  getPromotion,
  youxuan,
  cilckRecord,
  listActivities
} from '../../utils/api'

import {
  auth
} from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    attentions: [],
    mainActs: [],
    acts: [],
    images: [],
    skus: [],
    notices: [],
    show: false,
    pageNO: 1,
    latitude: '',
    longitude: '',
    isOver: false,
    loading: true,
    platform: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    text({
      type: 3
    }).then(res => {
      this.setData({
        attentions: res.data
      })
    })

    text({
      type: 5
    }).then(res => {
      this.setData({
        notices: res.data
      })
    })
    this.listActivities()
    this.auth()
  },


  /**
   * 跳转到小程序
   * @param {*} e 
   */
  navigateToMiniProgram(e) {
    this.getPromotion(e.currentTarget.dataset.actid).then(res => {
      wx.navigateToMiniProgram({
        appId: res.appId,
        path: res.pagePath,
        success: res => {
          cilckRecord({
            clickType: e.currentTarget.dataset.actid
          })
        }
      })
    })
  },
  /**
   * 查询推广信息
   */
  getPromotion(actId) {
    wx.showLoading({
      title: '加载中',
    })
    return new Promise((resolve, reject) => {
      getPromotion({
        platform: this.data.platform,
        actId: actId
      }).then(res => {
        wx.hideLoading({})
        resolve(res.data)
      })
    })
  },
  /**
   * 查询美团平台活动信息
   */
  listActivities() {
    listActivities({
      platform: this.data.platform
    }).then(res => {
      if (res.data) {
        const mainActs = res.data.filter(item => {
          if (item.mainAct === 1) {
            return item
          }
        })
        const acts = res.data.filter(item => {
          if (item.mainAct === 0) {
            return item
          }
        })
        const images = res.data.map(item => {
          return item.image
        })
        this.setData({
          mainActs: mainActs,
          acts: acts,
          images: images
        })
        for (let index = 0; index < this.data.mainActs.length; index++) {
          const act = this.data.mainActs[index]
          this.getPromotion(act.actId).then(res => {
            this.setData({
              ['mainActs[' + index + '].miniCode']: res.miniCode
            })
          })
        }
      }
      this.setData({
        loading: false
      })
    })
  },
  /**
   * 获取位置信息
   */
  auth() {
    auth('scope.userLocation', '请允许使用您的位置').then(res => {
      if (res) {
        this.getLocation().then(res => {
          if (res) {
            this.setData({
              isOver: false
            })
            this.youxuan()
          } else {
            this.setData({
              isOver: true,
            })
            wx.setStorageSync('location', false)
          }
        })
      } else {
        this.setData({
          show: false,
          isOver: true
        })
      }
    })
  },
  // 优选
  youxuan() {
    if (this.data.isOver) {
      return false
    }
    this.setData({
      show: true
    })
    youxuan({
      pageNO: this.data.pageNO++,
      pageSize: 30,
      latitude: this.data.latitude,
      longitude: this.data.longitude
    }).then(res => {
      if (res.data) {
        this.setData({
          skus: this.data.skus.concat(res.data)
        })
      } else {
        this.setData({
          isOver: true,
          show: false
        })
      }
    })
  },
  // 上拉触底函数
  onReachBottom: function () {
    if (!this.data.isOver) {
      setTimeout(() => {
        this.youxuan()
      }, 100);
    }
  },
  // 美团优选购买
  youXuanBuy(e) {
    wx.navigateToMiniProgram({
      appId: 'wx77af438b3505c00e',
      path: this.data.skus[e.currentTarget.dataset.index].itemWXLinkUrl,
      success: res => {
        cilckRecord({
          clickType: 1000
        })
      }
    })
  },
  /**
   * 获取位置
   */
  getLocation() {
    return new Promise((resolve, reject) => {
      wx.getLocation({
        success: res => {
          this.setData({
            latitude: res.latitude,
            longitude: res.longitude,
          })
          resolve(true)
        },
        fail: () => {
          resolve(false)
        }
      })
    })
  }
})